import java.io.InputStream;
import java.io.IOException;

public class RequestAction {
    private InputStream input;
    private String uri;

    public RequestAction(InputStream serverInput) {
	input = serverInput;
    }

    public void parse() {
	StringBuffer request = new StringBuffer(2048);
	byte[] buffer = new byte[2048];
	int bytesRead;

	try {
	    bytesRead = input.read(buffer);
	}
	catch(IOException e) {
	    System.out.println("A failure happened under request parsing.");
			       //+ e.printStackTrace());
	    bytesRead = -1;
	}

	for(int i = 0; i < bytesRead; i++) {
	    request.append((char)buffer[i]);
	}

	// For debugging 
	System.out.println("Request URI: \n" + request.toString());
	uri = parseURI(request.toString());
	System.out.println(uri);
    }

    private String parseURI(String requestString) {
	int indexA, indexB;

	indexA = requestString.indexOf(' ');
	if(indexA != -1) {
	    indexB = requestString.indexOf(' ', indexA + 1);
	    if(indexB > indexA) {
		return requestString.substring(indexA + 1, indexB);
	    }
	}
	return null;
    }

    public String getURI() {
	return uri;
    }
    
    
    
    
}
